package com.erp.wallet.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.wallet.domain.ErpSellerStoreWalletExpend;
import com.erp.wallet.domain.bo.ErpSellerStoreWalletExpendBo;
import com.erp.wallet.domain.bo.ErpSellerStoreWalletLogBo;
import com.erp.wallet.domain.vo.ErpSellerStoreWalletExpendVo;
import com.erp.wallet.mapper.ErpSellerStoreWalletExpendMapper;
import com.erp.wallet.service.IErpSellerStoreWalletExpendService;
import com.erp.wallet.service.IErpSellerStoreWalletLogService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 用户钱包拓展Service业务层处理
 *
 * @author erp
 * @date 2023-08-02
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreWalletExpendServiceImpl implements IErpSellerStoreWalletExpendService {

    private final ErpSellerStoreWalletExpendMapper baseMapper;
    private final IErpSellerStoreWalletLogService iErpSellerStoreWalletLogService;

    /**
     * 查询用户钱包拓展
     */
    @Override
    public ErpSellerStoreWalletExpendVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询用户钱包拓展列表
     */
    @Override
    public TableDataInfo<ErpSellerStoreWalletExpendVo> queryPageList(ErpSellerStoreWalletExpendBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStoreWalletExpend> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStoreWalletExpendVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户钱包拓展列表
     */
    @Override
    public List<ErpSellerStoreWalletExpendVo> queryList(ErpSellerStoreWalletExpendBo bo) {
        LambdaQueryWrapper<ErpSellerStoreWalletExpend> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStoreWalletExpend> buildQueryWrapper(ErpSellerStoreWalletExpendBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerStoreWalletExpend> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, ErpSellerStoreWalletExpend::getUserId, bo.getUserId());
        lqw.eq(bo.getEvaluate() != null, ErpSellerStoreWalletExpend::getEvaluate, bo.getEvaluate());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerStoreWalletExpend::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增用户钱包拓展
     */
    @Override
    public Boolean insertByBo(ErpSellerStoreWalletExpendBo bo) {
        ErpSellerStoreWalletExpend add = BeanUtil.toBean(bo, ErpSellerStoreWalletExpend.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改用户钱包拓展
     */
    @Override
    public Boolean updateByBo(ErpSellerStoreWalletExpendBo bo) {
        ErpSellerStoreWalletExpend update = BeanUtil.toBean(bo, ErpSellerStoreWalletExpend.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStoreWalletExpend entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除用户钱包拓展
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    public ErpSellerStoreWalletExpend getWalletExpend(Long userId) {
        ErpSellerStoreWalletExpend walletExpend = baseMapper.selectOne(new LambdaQueryWrapper<ErpSellerStoreWalletExpend>().eq(ErpSellerStoreWalletExpend::getUserId, userId));
        if (walletExpend == null) {
            walletExpend = new ErpSellerStoreWalletExpend();
            walletExpend.setEvaluate(BigDecimal.ZERO);
            walletExpend.setUserId(userId);
            baseMapper.insert(walletExpend);
        }
        return walletExpend;
    }

    @Override
    public R<Object> updateAmount(Integer flowType, Integer expenseType, Integer payType, Long sellerId, BigDecimal money, String orderNo, String remark) {

        ErpSellerStoreWalletExpend walletExpend = getWalletExpend(sellerId);
        BigDecimal evaluate = walletExpend.getEvaluate();
        if (flowType.equals(0)) { // 入款+
            evaluate = evaluate.add(money);
        } else if (flowType.equals(1)) { // 扣款-
            evaluate = evaluate.subtract(money);
        }else {
            return R.ok();
        }
        walletExpend.setEvaluate(evaluate);
        baseMapper.updateById(walletExpend);

        // 保存钱包日志
        ErpSellerStoreWalletLogBo log = new ErpSellerStoreWalletLogBo();
        log.setFlowType(flowType);
        log.setExpenseType(expenseType);
        log.setPayType(payType);
        log.setOrderNumber(orderNo);
        log.setMoney(money);
        log.setSellerId(sellerId);
        log.setRemark(remark);
        Boolean insertByBo = iErpSellerStoreWalletLogService.insertByBo(log);
        return insertByBo ? R.ok() : R.fail();
    }
}
